unit DFrame.LoggerFactory;

interface

uses
  DFrame.Logger;

type

LoggerFactory = class sealed

strict private
  constructor Create;

public

  class function getLogger(const name:String):ILogger; overload; static;

  class function getLogger(const clazz:TClass):ILogger; overload; static;

  class function getILoggerFactory():ILoggerFactory; static;
  class procedure setILoggerFactory(factory:ILoggerFactory); static;
end;

implementation

uses
  Windows, SysUtils, DFrame.DefaultLogger, DFrame.Reflection;

var
  loggerFactoryInstance : ILoggerFactory;

{ LoggerFactory }

class function LoggerFactory.getILoggerFactory: ILoggerFactory;
begin
  if not assigned(loggerFactoryInstance) then
    loggerFactoryInstance := TDefaultLoggerFactory.Create();
  Result := loggerFactoryInstance;
end;

class function LoggerFactory.getLogger(const name: String): ILogger;
begin
  Result := getILoggerFactory().getLogger(name);
end;

class function LoggerFactory.getLogger(const clazz: TClass): ILogger;
begin
  Result := getILoggerFactory().getLogger(clazz.ClassName);
end;


class procedure LoggerFactory.setILoggerFactory(factory: ILoggerFactory);
var
  oldFactory : ILoggerFactory;
begin
  if Assigned(factory) then begin
    oldFactory := loggerFactoryInstance;
    loggerFactoryInstance := factory;
    oldFactory := nil;
  end;
end;

constructor LoggerFactory.Create;
begin
end;

initialization
  {$IFDEF DEBUG}OutputDebugString('init logger factory ok');{$ENDIF}

finalization

  {$IFDEF DEBUG}OutputDebugString('finalize logger factory');{$ENDIF}
  if assigned(loggerFactoryInstance) then begin
    //TReflection.AsObject(loggerFactoryInstance).Free;
    loggerFactoryInstance := nil;
    //FreeAndNil(loggerFactoryInstance);
  end;
  {$IFDEF DEBUG}OutputDebugString('finalize logger factory ok');{$ENDIF}

end.
